package com.ruoyi.project.wx.service.impl;


import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.common.utils.bootdo.UuidUtil;
import com.ruoyi.project.wx.dao.GoodsProductDao;
import com.ruoyi.project.wx.dao.MemberCartDao;
import com.ruoyi.project.wx.dao.MemberUserDao;
import com.ruoyi.project.wx.domain.GoodsProductDO;
import com.ruoyi.project.wx.domain.MemberCartDO;
import com.ruoyi.project.wx.service.MemberCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class MemberCartServiceImpl implements MemberCartService {
	@Autowired
	private MemberCartDao memberCartDao;
	@Autowired
	private GoodsProductDao goodsProductDao;
	@Autowired
	private MemberUserDao memberUserDao;

	
	@Override
	public MemberCartDO get(String id){
		return memberCartDao.get(id);
	}
	
	@Override
	public List<MemberCartDO> list(Map<String, Object> map){
		return memberCartDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return memberCartDao.count(map);
	}
	
	@Override
	public int save(MemberCartDO memberCart){
		return memberCartDao.save(memberCart);
	}
	
	@Override
	public int update(MemberCartDO memberCart){
		return memberCartDao.update(memberCart);
	}
	
	@Override
	public int remove(String id){
		return memberCartDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return memberCartDao.batchRemove(ids);
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public R add(Map<String, Object> map) {
		String userId = map.get("userId").toString();
		String goodsId = map.get("goodsId").toString();
		String productId = map.get("productId").toString();
		int num = Integer.parseInt(map.get("num").toString());
//		BigDecimal price = new BigDecimal(map.get("price").toString());
		GoodsProductDO product = goodsProductDao.get(productId);
		Integer stock = product.getStock();
		if (num>stock){
			return R.error("库存不足");
		}
		//查询这个用户的购物车
		MemberCartDO memberCartDO = memberCartDao.queryByMemberGoodsAndProduct(map);
		//判断是否为空
		if (memberCartDO != null){
			int cartNum = memberCartDO.getNum();
			num = num+cartNum;
			memberCartDO.setNum(num);
//			BigDecimal allMoney =  productPrice.multiply(new BigDecimal(num));
//			memberCartDO.setPrice(allMoney);
			memberCartDao.update(memberCartDO);
		}else {
			memberCartDO = new MemberCartDO();
			memberCartDO.setId(UuidUtil.get32UUID());
			memberCartDO.setUserId(userId);
			memberCartDO.setGoodsId(goodsId);
			memberCartDO.setProductId(productId);
			memberCartDO.setOpAt(new Date());
			//sku
			GoodsProductDO goodsProductDO = goodsProductDao.get(productId);
			if (goodsProductDO==null){
				return R.error("产品异常");
			}

			memberCartDO.setSku(goodsProductDO.getSku());
			memberCartDO.setNum(num);
			memberCartDO.setDelFlag(0);
			memberCartDao.save(memberCartDO);
		}
		return R.ok();
	}

	@Override
	public List<Map<String, Object>> getCartListBuyUser(Map<String, Object> map) {
		List<Map<String, Object>> cartListBuyUser = memberCartDao.getCartListBuyUser(map);
		/*if(cartListBuyUser.size()>0){
			MemberUserDO memberUserDO = memberUserDao.get(map.get("userId").toString());
			for (Map<String, Object> map2 : cartListBuyUser) {
				if(memberUserDO.getLevel()=='A'){
					map2.put("price", map2.get("price_a").toString());
				}else if (memberUserDO.getLevel()=='B'){
					map2.put("price", map2.get("price_b").toString());
				}else if (memberUserDO.getLevel()=='C'){
					map2.put("price", map2.get("price_c").toString());
				}
			}
		}*/
		
		
		return cartListBuyUser;
	}

	@Override
	public int removeByUserIdAndCardId(Map<String, Object> map) {
		return memberCartDao.removeByUserIdAndCardId(map);
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public R editCart(Map<String, Object> map) {
		String cartId = map.get("cartId").toString();
		String userId = map.get("userId").toString();
		Integer num = Integer.valueOf(map.get("num").toString());
		if(num<1){
			return R.error("商品数量不能小于1");
		}
		MemberCartDO memberCartDO = memberCartDao.get(cartId);
		if (memberCartDO==null){
			return R.error("商品信息异常");
		}
		if (!userId.equals(memberCartDO.getUserId())){
			return R.error("用户信息异常");
		}
		memberCartDO.setNum(num);
		String productId = memberCartDO.getProductId();
		GoodsProductDO productDO = goodsProductDao.get(productId);
		if (productDO==null){
			return R.error("产品信息异常");
		}
		if (productDO.getDisabled()==1){
			return R.error("商品已下架");
		}
		int stock = productDO.getStock();
		if (num>stock){
			memberCartDO.setNum(stock);
			memberCartDao.update(memberCartDO);
			return R.error("库存不足");
		}
		//判断购买量
		/*int min = productDO.getBuyMin();
		int max = productDO.getBuyMax();
		if (num < min) {
			return R.error("该商品最小购买量为"+min);
		} else if (num > max) {
			return R.error("该商品最大购买量为"+max);
		}*/

		memberCartDao.update(memberCartDO);
		return R.ok();
	}

	@Override
	public List<Map<String, Object>> getCartListBuyIds(List<String> list) {
		return memberCartDao.getCartListBuyIds(list);
	}

    @Override
    public R getCountCart(Map<String, Object> map) {
		map.put("delFlag","0");
		int count = memberCartDao.sumCartNum(map);
		return R.ok().put("data",count);


    }


}
